﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using LGSVR.Common;
using System.Data;

namespace LGSVR.BLL.Sys
{
    public class ParamList
    {
        public DataTable GetListByTree()
        {
            string sql = "select * from paramList where state=@state";
            List<SqlParameter> lstParam = new List<SqlParameter>();
            lstParam.Add(new SqlParameter("@state", 1));

            DataTable dt = SQLHelper.GetDataTable(sql, System.Data.CommandType.Text, lstParam.ToArray());
            DataTable newDt = new DataTable();
            newDt.Columns.Add("typeName");
            newDt.Columns.Add("typeCode");

            ConvertDt("", dt, newDt,0);

            return newDt;
        }
        private DataTable ConvertDt(string typeBlong, DataTable dt, DataTable newDt,int level)
        {
            int Plot = 1;
            string strWhere = string.Empty;
            if (typeBlong == "")
            {
                strWhere = "typeBelong is null or typeBelong=''";

            }
            else
            { 
                strWhere = "typeBelong='" + typeBlong+"'";
            }
            level++;
            foreach (DataRow dr in dt.Select(strWhere))
            {
                Plot = level * 2;
                int sLen = (int)dr["typeName"].ToString().Length;

                DataRow newRow = newDt.NewRow();
                newRow["typeName"] = dr["typeName"].ToString().PadLeft(Plot + sLen, '　'); 
                newRow["typeCode"] = dr["typeCode"];
                newDt.Rows.Add(newRow);
                ConvertDt(dr["typeCode"].ToString(), dt, newDt, level);
            }
            return newDt;
        }

        public DataTable GetListByBelong(string typeBelong)
        {
            string sql = "select * from paramList where typebelong=@typebelong";
            List<SqlParameter> lstParam = new List<SqlParameter>();
            lstParam.Add(new SqlParameter("@typebelong", typeBelong));

            DataTable dt = SQLHelper.GetDataTable(sql, System.Data.CommandType.Text, lstParam.ToArray());
            return dt;
        }

    }
}
